<?php

namespace App\Admin\Actions\Grid;

use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Models\CabinetAlarmLog;
use Dcat\Admin\Admin;

class HandleCabinetAlarmLog extends RowAction
{
    /**
     * @return string
     */
    protected $title = '&nbsp;<i class="fa fa-check"></i>&nbsp;';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $log = CabinetAlarmLog::find($this->getKey());
        $log->status = CabinetAlarmLog::STATUS_YES;
        $log->operator = Admin::user()->id;
        if ($log->save()) {
            return $this->response()->success('处理成功')->refresh();
        } else {
            return $this->response()->error('处理失败')->refresh();
        }
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        $log = CabinetAlarmLog::find($this->getKey());
        $title = '确认处理-'.$log['alarm_name'].'？';

        return [$title];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}
